package org.infodavid.common.notifier.impl;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import org.infodavid.common.notifier.IAttachment;
import org.infodavid.common.notifier.IMailCommand;

/**
 * The Class MailCommandImpl.
 */
public class MailCommandImpl extends AbstractNotifierCommand implements IMailCommand {

	/** serialVersionUID. */
	private static final long serialVersionUID = -2067794727906259469L;

	/** The attachments. */
	private final Set<IAttachment> attachments = new HashSet<IAttachment>();

	/** The bcc. */
	private final Set<String> bcc = new HashSet<String>();

	/** The cc. */
	private final Set<String> cc = new HashSet<String>();

	/** The content. */
	private IAttachment content = null;

	/** The asSeparate. */
	private boolean separate = false;

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.notifier.IMailCommand#getBcc()
	 */
	public Set<String> getBcc() {
		return bcc;
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.notifier.IMailCommand#getCc()
	 */
	public Set<String> getCc() {
		return cc;
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.notifier.IMailCommand#getAttachments()
	 */
	public Set<IAttachment> getAttachments() {
		return attachments;
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see
	 * org.infodavid.common.notifier.IMailCommand#addAttachment(org.infodavid.common.notifier.IAttachment)
	 */
	public void addAttachment(final IAttachment value) {
		getAttachments().add(value);
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.notifier.IMailCommand#addAttachment(java.lang.String, byte[])
	 */
	public void addAttachment(final String name, final byte[] data) {
		final IAttachment attachment = newAttachmentInstance();

		attachment.setName(name);
		attachment.setData(data);
		attachment.setEncoding(IAttachment.DEFAULT_ENCODING);
		attachment.setMimeType(IAttachment.DEFAULT_MIME_TYPE);

		addAttachment(attachment);
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see
	 * org.infodavid.common.notifier.IMailCommand#setAttachments(org.infodavid.common.notifier.IAttachment
	 * [])
	 */
	public void setAttachments(final IAttachment... values) {
		attachments.clear();

		if (!(values == null || values.length == 0)) {
			for (final IAttachment item : values) {
				attachments.add(item);
			}
		}
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.notifier.IMailCommand#setBcc(java.lang.String[])
	 */
	public void setBcc(final String... values) {
		bcc.clear();

		if (!(values == null || values.length == 0)) {
			for (final String item : values) {
				bcc.add(item);
			}
		}
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.notifier.IMailCommand#setCc(java.lang.String[])
	 */
	public void setCc(final String... values) {
		cc.clear();

		if (!(values == null || values.length == 0)) {
			for (final String item : values) {
				cc.add(item);
			}
		}
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.notifier.ITextCommand#getContent()
	 */
	public IAttachment getContent() {
		return content;
	}

	public void addData(final byte[] value) {
		if (getContent() == null) {
			synchronized (this) {
				setContent(new AttachmentImpl());
			}

			getContent().setData(value);
		}
		else {
			final IAttachment attachment = new AttachmentImpl();

			attachment.setData(value);
			addAttachment(attachment);
		}
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.notifier.ITextCommand#setContent(java.lang.String)
	 */
	public void setContent(final IAttachment value) {
		content = value;
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.notifier.IMailCommand#setSeparate(boolean)
	 */
	public void setSeparate(final boolean flag) {
		separate = flag;
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.notifier.IMailCommand#isSeparate()
	 */
	public boolean isSeparate() {
		return separate;
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.notifier.IMailCommand#newAttachmentInstance()
	 */
	public IAttachment newAttachmentInstance() {
		return new AttachmentImpl();
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
  public String toString() {
		final StringBuilder builder = new StringBuilder();

		builder.append(super.toString());
		builder.append("MailCommandImpl [bcc=");
		builder.append(bcc);
		builder.append(", cc=");
		builder.append(cc);
		builder.append(", content=");
		builder.append(content);
		builder.append(", separate=");
		builder.append(separate);
		builder.append(", attachments=");

		if (attachments == null) {
			builder.append("null");
		}
		else {
			builder.append(Arrays.toString(attachments.toArray()));
		}

		builder.append("]");

		return builder.toString();
	}
}
